﻿using KJSyncXP.Extensions;
using KJSyncXP.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KJSyncXP.Views
{
    /// <summary>
    /// CheckPoint.xaml 的交互逻辑
    /// </summary>
    public partial class CheckPoint : UserControl
    {
        public CheckPointViewModel _viewModel;

        public CheckPoint(CheckPointViewModel viewModel)
        {
            InitializeComponent();
            _viewModel = viewModel;
            this.DataContext = viewModel;
        }

        private void CheckPoint_Loaded(object sender, RoutedEventArgs e)
        {
            // 加载权限
            UpdateTabPermissions();
        }

        private void UpdateTabPermissions()
        {
            var authManager = AuthenticationManager.Instance;

            foreach (TabItem tabItem in CheckTab.Items)
            {
                if (tabItem.Tag is string permKey && !string.IsNullOrEmpty(permKey))
                {
                    // 调用权限验证方法，设置IsEnabled（true=可用，false=禁用）
                    tabItem.IsEnabled = authManager.authorize(permKey);
                }
                else
                {
                    // 无权限标识的选项卡，默认启用
                    tabItem.IsEnabled = true;
                }
            }
            // 确保选中一个“可用”的选项卡
            SelectFirstEnabledTab();
        }

        /// <summary>
        /// 选中第一个“可用”的选项卡
        /// </summary>
        private void SelectFirstEnabledTab()
        {
            foreach (TabItem tabItem in CheckTab.Items)
            {
                if (tabItem.IsEnabled)
                {
                    CheckTab.SelectedItem = tabItem;
                    break;
                }
            }
        }

        private void PermissionQueryButton_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = AuthenticationManager.Instance.authorize("PermissionQueryButton");
        }

        private void StandardProductButton_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = AuthenticationManager.Instance.authorize("StandardProductButton");
        }

       
        private void SearchStandrad(object sender, ExecutedRoutedEventArgs e)
        {
            // 调用ViewModel中的查询方法
            _viewModel?.SearchStandrad();
        }

        private void ShowStandrad(object sender, ExecutedRoutedEventArgs e)
        {
            // 调用ViewModel中的查询方法
            _viewModel?.ShowStandrad();
        }


    }
}
